package com.swang.pms.service.manage.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.swang.pms.dal.ManageMenuDao;
import com.swang.pms.model.common.GridUtil;
import com.swang.pms.model.common.Pager;
import com.swang.pms.model.dto.ManageMenuDto;
import com.swang.pms.model.entity.DhtmlxTreeMapXml;
import com.swang.pms.model.entity.ManageMenu;
import com.swang.pms.service.manage.ManageMenuService;

/**
 * @ClassName: ManageMenuServiceImpl
 * @Description: 菜单服务实现类
 * @author 王泽
 * @date 2017年3月16日 下午9:45:51
 */
@Service("manageMenuService")
public class ManageMenuServiceImpl implements ManageMenuService {

    @Resource
    private ManageMenuDao manageMenuDao;

    public Boolean addManageMenu(ManageMenu ManageMenu) {
        return this.manageMenuDao.insert(ManageMenu) == 1;
    }

    public Boolean addManageMenuSelective(ManageMenu ManageMenu) {
        return this.manageMenuDao.insertSelective(ManageMenu) == 1;
    }

    public Boolean deleteManageMenuByKey(Long ManageMenuId) {
        return this.manageMenuDao.deleteByPrimaryKey(ManageMenuId) == 1;
    }

    public Boolean updateManageMenuByKey(ManageMenu ManageMenu) {
        return this.manageMenuDao.updateByPrimaryKey(ManageMenu) == 1;
    }

    public Boolean updateManageMenuByKeySelective(ManageMenu ManageMenu) {
        return this.manageMenuDao.updateByPrimaryKeySelective(ManageMenu) == 1;
    }

    public Boolean updateManageMenuByDto(ManageMenuDto ManageMenuDto) {
        return true;
    }

    public ManageMenu selectManageMenuByKey(Long ManageMenuId) {
        return this.manageMenuDao.selectByPrimaryKey(ManageMenuId);
    }

    //扩展
    public int addManageMenuByDto(ManageMenuDto dto) {
        return 0;
    }

    public List<ManageMenuDto> selectsBySelf(ManageMenuDto dto) {
        return this.manageMenuDao.selectsBySelf(dto);
    }

    //分页
    public Pager findPageDataByDto(Pager page, ManageMenuDto dto) {

        String[] paramSerial = new String[] { "id", "parent_id", "menu_type", "menu_name", "menu_url", "picture_url", "sort_num", "status",
                "remark", "gmt_create" };
        dto.setColumns(Arrays.asList(paramSerial).toString().replaceAll("[\\[\\]\\s]", ""));
        dto.setStartIndex(page.getRowStartIndex());
        dto.setPageSize(page.getPageSize());
        List<Map<String, Object>> list = this.manageMenuDao.findPageDataByDto(dto);

        dto.setStartIndex(null);
        dto.setPageSize(null);
        page.setRowCount(this.manageMenuDao.findPageDataCountByDto(dto));
        page.setGridr(GridUtil.getGridList(list, paramSerial, page.getRowStartIndex(), page.getPageSize(), page.getRowCount(), true));
        return page;
    }

    //菜单树
    public String getMenuTree(Long id, String userName) {
        List<Map<String, Object>> dbMaps = new ArrayList<Map<String, Object>>();
        if ("admin".equals(userName)) {
            dbMaps = this.manageMenuDao.getLeftMenuMaps();
        } else {
            dbMaps = this.manageMenuDao.getLeftMenuMapsById(id);
        }
        return DhtmlxTreeMapXml.generateTreeMapXml(dbMaps);
    }

    //菜单权限
    public List<String> getMenusById(Long id, String userType) {
        if (id == null) {
            return null;
        }
        if (userType != null && userType.equals("sysadmin")) {
            return this.manageMenuDao.get2Menus();
        } else {
            return this.manageMenuDao.get2MenusById(id);
        }
    }

    public int getManageMenuCount(ManageMenu manageMenu) {
        return this.manageMenuDao.getManageMenuCount(manageMenu);
    }

    @Override
    public List<ManageMenuDto> getLeftMenuList(Long id) {
        return this.manageMenuDao.getLeftMenuList(id);
    }
}
